using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Manager class for the workflow processes start actions within a process
	/// </summary>
	public class ProcessStarts : BaseManager
	{
		#region Constructors
		public ProcessStarts() : base() { }
		public ProcessStarts(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public ProcessStarts(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Retrieval
		/// <summary>
		/// Get all process start actions for the specified process ID
		/// </summary>
		/// <param name="ProcessID"></param>
		/// <returns></returns>
		public ProcessStart [] GetAllProcessStartForProcessID(int ProcessID)
		{
			DataTable results = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFProcessStartGetByProcessID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ProcessID));
			da.Fill(results);
			ReleaseConnection();

			List<ProcessStart> list = new List<ProcessStart>();
			foreach (DataRow r in results.Rows)
				list.Add(new ProcessStart(r));

			return list.ToArray();
		}
		/// <summary>
		/// Gets a process start action by it's own ID
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public ProcessStart GetProcessStartByID(int ID)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFProcessStartGetByID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new ProcessStart(result.Rows[0]);
		}
		/// <summary>
		/// Gets a process start action for a process ID and rank position
		/// </summary>
		/// <param name="ProcessID"></param>
		/// <param name="Rank"></param>
		/// <returns></returns>
		public ProcessStart GetProcessStartByProcessAndRank(int ProcessID, int Rank)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFProcessStartGetByProcessIDAndRank");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ProcessID));
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@rank", SqlDbType.Int, 4, Rank));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new ProcessStart(result.Rows[0]);
		}
		#endregion
		#region Utilities
		/// <summary>
		/// Gets the next available rank number
		/// </summary>
		/// <param name="ProcessID"></param>
		/// <returns></returns>
		public int GetNextRank(int ProcessID)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFProcessStartGetNextRankForProcessID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ProcessID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return 1;
			if (result.Rows.Count < 1) return 1;

			int LastRank = SQLUtility.DBNull( result.Rows[0]["LastRank"], 0 );
			return LastRank+1;
		}
		/// <summary>
		/// Shuffles a startup action up the ranks
		/// </summary>
		/// <param name="ProcessStartID"></param>
		public void ShuffleUp(int ProcessStartID)
		{
			ProcessStart source = this.GetProcessStartByID( ProcessStartID );
			if( source == null ) return;
			if( source.Rank <= 1 ) return;

			ProcessStart target = this.GetProcessStartByProcessAndRank( source.FK_ProcessID, source.Rank - 1 );
			if( target == null ) return;

			int temp = source.Rank;
			source.Rank = target.Rank;
			target.Rank = temp;

			Save(source);
			Save(target);
		}
		/// <summary>
		/// Shuffles a startup action down the ranks
		/// </summary>
		/// <param name="ProcessStartID"></param>
		public void ShuffleDown(int ProcessStartID)
		{
			ProcessStart source = this.GetProcessStartByID( ProcessStartID );
			if( source == null ) return;
			
			ProcessStart target = this.GetProcessStartByProcessAndRank( source.FK_ProcessID, source.Rank + 1 );
			if( target == null ) return;

			int temp = source.Rank;
			source.Rank = target.Rank;
			target.Rank = temp;

			Save(source);
			Save(target);
		}
		#endregion
		#region Save Records
		/// <summary>
		/// Save the specified process start action to the database
		/// </summary>
		/// <param name="ps"></param>
		/// <returns></returns>
		public int Save(ProcessStart ps)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFProcessStartSave");
			SqlParameter result = SQLUtility.ParameterInOut("@id", SqlDbType.Int, 4, ps.PK_ID);
			cmd.Parameters.Add(result);

			cmd.Parameters.Add( SQLUtility.ParameterIn("@FK_ProcessID", SqlDbType.Int, 4, ps.FK_ProcessID));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@Rank", SqlDbType.Int, 4, ps.Rank ));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@Method", SqlDbType.NVarChar, 500, ps.Method));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@PropertyBag", SqlDbType.NText, 16, ps.PropertyBag));

			cmd.ExecuteNonQuery();
			ReleaseConnection();

			if (ps.PK_ID == -1)
				ps.PK_ID = (int)result.Value;

			return ps.PK_ID;
		}
		#endregion
		#region Delete Records
		/// <summary>
		/// Delete the specified process start action from the database
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public bool DeleteByID(int ID)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFProcessStartDeleteByID");
			cmd.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			cmd.ExecuteNonQuery();
			ReleaseConnection();
			return true;
		}
		#endregion
	}
}
